#ifndef GPU_INCREMENTALINTEGRATOR_H
#define GPU_INCREMENTALINTEGRATOR_H

#include <IncrementalIntegrator.h>
#include "GPU_FormUnbalance.h"
#include <memory>
#include <chrono>

/**
 * GPU-accelerated Incremental Integrator for OpenSees
 * Provides GPU-accelerated formUnbalance for significant performance improvements
 * in large-scale models (>10000 DOF)
 */
class GPU_IncrementalIntegrator : public IncrementalIntegrator {
public:
    GPU_IncrementalIntegrator(int maxElements = 50000, int maxDOFs = 100000, int maxNodes = 50000);
    virtual ~GPU_IncrementalIntegrator();

    // Override formUnbalance to use GPU implementation
    virtual int formUnbalance(void) override;
    
    // Configuration methods
    void enableGPU(bool enable) { gpuEnabled = enable; }
    void enableProfiling(bool enable);
    
    // Performance monitoring
    double getLastGPUTime() const;
    double getLastCPUTime() const;
    void printPerformanceStats() const;
    
    // Memory management
    int setMaxProblemSize(int maxElements, int maxDOFs, int maxNodes);
    
protected:
    // GPU acceleration object
    std::unique_ptr<GPU_FormUnbalance> gpuFormUnbalance;
    
    // Configuration flags
    bool gpuEnabled;
    bool profilingEnabled;
    
    // Performance tracking
    mutable double lastGPUTime;
    mutable double lastCPUTime;
    mutable int gpuCallCount;
    mutable int cpuCallCount;
    mutable double totalGPUTime;
    mutable double totalCPUTime;
    
    // Fallback to CPU implementation if GPU fails
    int formUnbalanceCPU(void);
    
    // Adaptive switching between GPU and CPU
    bool shouldUseGPU() const;
    
private:
    // Timing utilities
    std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
    void startTiming();
    double stopTiming();
};

#endif // GPU_INCREMENTALINTEGRATOR_H